package com.wawei.seeds.services;

import android.content.Context;
import android.os.Handler;
import android.util.Log;

import com.wawei.seeds.model.CommentModel;
import com.wawei.seeds.services.remote.CommentRestfulWrapper;
import com.wawei.seeds.services.remote.WalnutCommentService;
import com.wawei.seeds.utils.NetBase;
import com.wawei.seeds.utils.RetrofitNetworkService;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by itachi on 15/8/2.
 */
public class WalnutCommentServiceHandler {
    private Context context;

    public WalnutCommentServiceHandler(Context context) {
        this.context = context;
    }

    public void retrieveReviews(RestAdapter restAdapter, String providerId, final Handler handler, final Integer handlerTag) {
        WalnutCommentService service = restAdapter.create(WalnutCommentService.class);
        service.reviews(providerId, new Callback<CommentRestfulWrapper>(){

            @Override
            public void success(CommentRestfulWrapper commentRestfulWrapper, Response response) {
                List<CommentModel> commentModels = commentRestfulWrapper.getReviews();

                if (commentModels != null && !commentModels.isEmpty()) {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, commentModels.toArray());
                } else {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, new ArrayList<>(0).toArray());
                }
            }

            @Override
            public void failure(RetrofitError error) {
                RetrofitNetworkService.handleError(handler, handlerTag, error);
            }
        });
    }

    public void share(RestAdapter restAdapter, int userId, String nickName, String providerId, String content, final Handler handler, final Integer handlerTag) {
        WalnutCommentService service = restAdapter.create(WalnutCommentService.class);
        service.share(userId, nickName, providerId, content, new Callback<NetBase>() {
            @Override
            public void success(NetBase netBase, Response response) {
                if (netBase.getResultCode() == 200) {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, "ok");
                } else {
                    RetrofitNetworkService.handleSuccess(handler, handlerTag, null);
                }
            }

            @Override
            public void failure(RetrofitError error) {
                RetrofitNetworkService.handleError(handler, handlerTag, error);
            }
        });
    }
}
